#include "itemwriter.h"

itemWriter::itemWriter(QTreeWidget* treeWidget)
    : treeWidget(treeWidget)
{
    writer.setAutoFormatting(true);
}

bool itemWriter::writeFile(QIODevice* device)
{
    writer.setDevice(device);
    itemCount = 0;

    writer.writeStartDocument();
    writer.writeStartElement("items");
    for (int i = 0; i < treeWidget->topLevelItemCount(); ++i)
    {
        writeItems(treeWidget->topLevelItem(i));
    }
    writer.writeEndElement();
    writer.writeEndDocument();

    return true;
}

void itemWriter::writeItems(QTreeWidgetItem* item)
{
    for (int i = 0; i < item->childCount(); i++)
    {
        writer.writeStartElement(item->text(0).toLower());
        writer.writeTextElement("itemName", item->child(i)->text(0));
        writer.writeTextElement("armor", item->child(i)->text(1));
        writer.writeTextElement("damage", item->child(i)->text(2));
        writer.writeTextElement("attackSpeed", item->child(i)->text(3));
        writer.writeTextElement("itemLevel", item->child(i)->text(4));
        writer.writeTextElement("requiredLevel", item->child(i)->text(5));
        writer.writeTextElement("rarity", item->child(i)->text(6));
        writer.writeTextElement("class", item->child(i)->text(7));
        writer.writeTextElement("strBonus", item->child(i)->text(8));
        writer.writeTextElement("dexBonus", item->child(i)->text(9));
        writer.writeTextElement("staBonus", item->child(i)->text(10));
        writer.writeTextElement("intBonus", item->child(i)->text(11));
        writer.writeTextElement("wilBonus", item->child(i)->text(12));
        writer.writeTextElement("maxCount", item->child(i)->text(13));
        writer.writeTextElement("description", item->child(i)->text(14));
        writer.writeTextElement("price", item->child(i)->text(15));
        writer.writeEndElement();
        itemCount++;
    }
}

int itemWriter::getItemCount()
{
    return itemCount;
}
